'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/sendmsg/pub/batch 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		var nowtime = new Date().getTime()

		//====从数据库获取token====
		const tokenFromDb = await db.collection('a_config').where({
			name: "tttoken"
		}).get()
		var access_token = tokenFromDb.data[0].token
		var expire_time = tokenFromDb.data[0].expire_time
		//====从数据库获取token结束====

		//====如果token已经过期，则重新获取====
		if (expire_time < nowtime) {
			let url = "https://developer.toutiao.com/api/apps/v2/token"
			const token = await uniCloud.httpclient.request(url, {
				method: 'POST',
				data: {
					"appid": "tt9efee84301c08f8e01",
					"secret": "ca292864c3a74d274b64137a9815191172a40ac2",
					"grant_type": "client_credential"
				},
				contentType: 'json', // 指定以application/json发送data内的数据
				dataType: 'json' // 指定返回值为json格式，自动进行parse
			})

			console.log("token=", token.data.data.access_token);

			db.collection('a_config').where({
				name: "tttoken"
			}).update({
				token: token.data.data.access_token,
				expire_time: nowtime + 7000000 //到期时间为当前时间加7000秒
			})
			//过期后重新获取并更新数据库的值
			var access_token = token.data.data.access_token
		}
		//====重新获取token结束====

		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			msgs,
			things
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		console.log("things=", things);
		let batchRunRes = await vk.pubfn.batchRun({
			// 主执行函数
			main: async (item, index) => {
				await vk.pubfn.sleep((Math.floor(Math.random() * (3 - 0)) + 0) * 100);

				//====判断消息类型，执行不同函数====
				if (item.msgType == "qdtx") {
					qdtx(access_token, item.openid)
				}
				if (item.msgType == "rbxj") {
					rbxj(access_token, item.openid)
				}
				if (item.msgType == "jjgx") {
					jjgx(access_token, item.openid)
				}
				//====判断消息类型结束====

				//====更改状态为已发送====
				const updtate = await db.collection('a_dingyue').doc(item._id).update({
					is_send: true
				})
				//====更改状态结束====

				return {
					code: 0,
					index
				}
			},
			// 最大并发量，如果设置为1，则会按顺序执行
			concurrency: 100,
			// 数据源，这些数据会依次跑一遍main函数
			data: msgs
		});


		// 业务逻辑结束-----------------------------------------------------------
		return res;

		async function qdtx(mytoken, openid) {
			// 签到提醒
			const pushmsg = {
				"access_token": mytoken,
				"app_id": "tt9efee84301c08f8e01",
				"tpl_id": "MSG13590956fe8f56c3dce95c82f7fab466a3e327a15885",
				"open_id": openid,
				"data": {
					"签到名称": things.thing1,
					"可领福利": things.thing2,
					"温馨提示": things.thing3,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function jjgx(mytoken, openid) {
			//剧集更新
			const pushmsg = {
				"access_token": mytoken,
				"app_id": "tt9efee84301c08f8e01",
				"tpl_id": "MSG135909541c9befe1cbe0be04995d9fb7f9bd99315721",
				"open_id": openid,
				"data": {
					"剧集标题": things.thing1,
					"更新集数": things.thing2,
					"更新时间": things.thing3,
					"描述": things.thing4,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function rbxj(mytoken, openid) {
			//热播新剧
			const pushmsg = {
				"access_token": mytoken,
				"app_id": "tt9efee84301c08f8e01",
				"tpl_id": "MSG1359095d8f91eb701102666783f5525f53793e915722",
				"open_id": openid,
				"data": {
					"热播新剧": things.thing1,
					"上线时间": things.thing3,
					"描述": things.thing2,
				},
				"page": "/pages/index/index"
			}
			tosend(pushmsg)
		}

		async function tosend(pushmsg) {
			let sendurl = "https://developer.toutiao.com/api/apps/subscribe_notification/developer/v1/notify";
			const send = await uniCloud.httpclient.request(sendurl, {
				method: 'POST',
				data: JSON.stringify(pushmsg),
				contentType: 'json', // 指定以application/json发送data内的数据
				dataType: 'json' // 指定返回值为json格式，自动进行parse
			})
			console.log("send=", send);
		}
	}
}
